package com.attilax.ioc;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import com.attilax.db.DbServiceV4qb9;
import com.attilax.io.pathx;
import com.attilax.persistence.DBCfg;

public class BeanFac {

	public static void main(String[] args) {
		DbServiceV4qb9 dbc = BeanFac.getBean(DbServiceV4qb9.class);
		String sql = "select * from city";
		System.out.println(dbc.findBySql(sql));
	}

	private static DbServiceV4qb9 getBean(Class<DbServiceV4qb9> class1) {
		if (class1 == DbServiceV4qb9.class)
			return new DbServiceV4qb9(getDbcfg());
		return null;
	}

	private static DBCfg getDbcfg() {
		DBCfg cfg = new DBCfg();
		try {
			// propertyReader pro=new Properties();

			FileInputStream fis = new FileInputStream(new File(pathx.webAppPath() + "/cfg_game2.properties"));
			Properties properties = new Properties();
			properties.load(fis);
			// pro.
			String url = properties.getProperty("jdbc.url");
			String driver = properties.getProperty("jdbc.driverClassName");

			String uname = properties.getProperty("jdbc.username");
			String pwd = properties.getProperty("jdbc.password");
			cfg.setUrl(url);
			cfg.setUser(uname);
			cfg.setPassword(pwd);
			cfg.setDriver(driver);
			return cfg;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("cant ini dbcfg", e);
		}

	}
}
